//
//  TBCWeatherView.m
//  待定
//
//  Created by BoyaLee on 15/11/5.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "TBCWeatherView.h"
#import "TBCWeather.h"
#import "TBCLocalWeather.h"
#import "TBCPM2D5.h"

@interface TBCWeatherView () <TBCWeather>

@property (strong, nonatomic) TBCWeather *weather;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UIImageView *bgImageView;
@property (strong, nonatomic) UILabel *dt_week;
@property (strong, nonatomic) UILabel *city;
@property (strong, nonatomic) UILabel *temperature;
@property (strong, nonatomic) UILabel *climate;
@property (strong, nonatomic) UILabel *wind;
@property (strong, nonatomic) UILabel *pm;
@property (strong, nonatomic) UIView *downView;

@property (copy, nonatomic) NSString *aqi;

@end

@implementation TBCWeatherView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self initTBCWeatherData];
        
    }
    
    return self;
    
}

- (void)initTBCWeatherViews {
    [self initTBCWeatherData];
    self.backgroundColor = [UIColor whiteColor];
    
    self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    self.bgImageView.image = [UIImage imageNamed:@"weather_default_bg"];
    [self addSubview:self.bgImageView];
    
    [self initWeatherUpViews];
    
}

- (void)initTBCWeatherData {
    
    [TBCWeather shareTBCWeather].delegate = self;
    
}

- (void)sendWeather:(TBCWeather *)weather {
    
    self.weather = [[TBCWeather alloc] init];
    self.weather = weather;
    NSLog(@"weather = %@", weather);
    
    [self initTBCWeatherViews];
    
}

- (void)initWeatherUpViews {
    
    NSString *string = self.weather.dt;
    NSRange monthRange = NSMakeRange(5, 2);
    NSString *month = [string substringWithRange:monthRange];
    NSRange dayRange = NSMakeRange(8, 2);
    NSString *day = [string substringWithRange:dayRange];
    
    NSMutableArray * allArray = [NSMutableArray array];
    allArray = self.weather.allWeatherArray;
    TBCLocalWeather *today = [allArray firstObject];
    
    NSString *week = today.week;
    
    self.dt_week = [[UILabel alloc] initWithFrame:CGRectMake(125 * WIDTH / 375, 5 * (HEIGHT - 64) / 603, 150 * WIDTH / 375, 30 * (HEIGHT - 64) / 603)];
    self.dt_week.text = [NSString stringWithFormat:@"%@月%@日 %@", month, day, week];
    self.dt_week.textColor = [UIColor whiteColor];
    self.dt_week.font = [UIFont boldSystemFontOfSize:15];
    [self.bgImageView addSubview:self.dt_week];
    
    self.city = [[UILabel alloc] initWithFrame:CGRectMake(150 * WIDTH / 375, 65 * (HEIGHT - 64) / 603, 100 * WIDTH / 375, 30 * (HEIGHT - 64) / 603)];
    self.city.text = self.cityName;
    self.city.textColor = [UIColor whiteColor];
    self.city.font = [UIFont boldSystemFontOfSize:30];
    [self.bgImageView addSubview:self.city];
    
    NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:0];
    int n = 0;
    NSString *nameStr = @"";
    
    if ([today.climate isEqualToString:@"多云"]) {
        
        n = 48;
        nameStr = @"sun_and_cloud";
        
    }else if ([today.climate isEqualToString:@"晴"]) {
        
        n = 101;
        nameStr = @"sun";
        
    }else if ([today.climate containsString:@"雨"]) {
        
        n = 62;
        nameStr = @"rain";
        
    }else {
        
        n = 29;
        nameStr = @"cloud";
        
    }
    
    if ([nameStr isEqualToString:@"sun"]) {
        
        for (int i = 1; i < n; i++) {
            
            if (i < 9) {
                
                NSString *name = [NSString stringWithFormat:@"%@000%d", nameStr, i + 1];
                UIImage *image = [UIImage imageNamed:name];
                [imageArray addObject:image];
                
            }else if (i < 99) {
                
                NSString *name = [NSString stringWithFormat:@"%@00%d", nameStr, i + 1];
                UIImage *image = [UIImage imageNamed:name];
                [imageArray addObject:image];
                
            }else {
                
                NSString *name = [NSString stringWithFormat:@"%@0%d", nameStr, i + 1];
                UIImage *image = [UIImage imageNamed:name];
                [imageArray addObject:image];
                
            }
            
        }
        
    }else {
        
        for (int i = 0; i < n; i++) {
            
            if (i < 9) {
                
                NSString *name = [NSString stringWithFormat:@"%@000%d", nameStr, i + 1];
                UIImage *image = [UIImage imageNamed:name];
                [imageArray addObject:image];
                
            }else {
                
                NSString *name = [NSString stringWithFormat:@"%@00%d", nameStr, i + 1];
                UIImage *image = [UIImage imageNamed:name];
                [imageArray addObject:image];
                
            }
            
        }
        
    }
    
    UIImageView *weatherImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80 * WIDTH / 375, 130 * (HEIGHT - 64) / 603, 100 * WIDTH / 375, 135 * (HEIGHT - 64) / 603)];
    
    if ([nameStr isEqualToString:@"sun"]) {
        
        weatherImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@0%d", nameStr, n]];
        
    }else {
        
        [weatherImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@00%d", nameStr, n]]];
        
    }
    
    weatherImageView.animationImages = imageArray;
    weatherImageView.animationDuration = imageArray.count * 0.1;
    weatherImageView.animationRepeatCount = 1;
    [weatherImageView startAnimating];
    [self.bgImageView addSubview:weatherImageView];
    
    self.temperature = [[UILabel alloc] initWithFrame:CGRectMake(190 * WIDTH / 375, 130 * (HEIGHT - 64) / 603, 150 * WIDTH / 375, 30 * (HEIGHT - 64) / 603)];
    self.temperature.text = today.temperature;
    self.temperature.textColor = [UIColor whiteColor];
    self.temperature.font = [UIFont systemFontOfSize:28];
    [self.bgImageView addSubview:self.temperature];
    
    self.climate = [[UILabel alloc] initWithFrame:CGRectMake(190 * WIDTH / 375, 160 * (HEIGHT - 64) / 603, 100 * WIDTH / 375, 30 * (HEIGHT - 64) / 603)];
    self.climate.text = today.climate;
    self.climate.textColor = [UIColor whiteColor];
    self.climate.font = [UIFont boldSystemFontOfSize:14];
    [self.bgImageView addSubview:self.climate];
    
    self.wind = [[UILabel alloc] initWithFrame:CGRectMake(190 * WIDTH / 375, 180 * (HEIGHT - 64) / 603, 100, 30 * (HEIGHT - 64) / 603)];
    self.wind.text = today.wind;
    self.wind.textColor = [UIColor whiteColor];
    self.wind.font = [UIFont systemFontOfSize:14];
    [self.bgImageView addSubview:self.wind];
    
    self.pm = [[UILabel alloc] initWithFrame:CGRectMake(190 * WIDTH / 375, 200 * (HEIGHT - 64) / 603, 150 * WIDTH / 375, 30 * (HEIGHT - 64) / 603)];
    TBCPM2D5 *pm = [[TBCPM2D5 alloc] init];
    pm = self.weather.pm;
    NSInteger pm2_5 = [pm.pm2_5 integerValue];
    
    if (pm2_5 <= 35) {
        
        self.aqi = @"优";
        
    }else if (pm2_5 > 35 && pm2_5 <= 75) {
        
        self.aqi = @"良";
        
    }else if (pm2_5 > 75 && pm2_5 <= 115) {
        
        self.aqi = @"轻度污染";
        
    }else if (pm2_5 > 115 && pm2_5 <= 150) {
        
        self.aqi = @"中度污染";
        
    }else if (pm2_5 > 150 && pm2_5 <= 250) {
        
        self.aqi = @"重度污染";
        
    }else if (pm2_5 > 250) {
        
        self.aqi = @"严重污染";
        
    }
    
    self.pm.text = [NSString stringWithFormat:@"PM2.5 %@ %@", pm.pm2_5, self.aqi];
    self.pm.textColor = [UIColor whiteColor];
    self.pm.font = [UIFont systemFontOfSize:14];
    [self.bgImageView addSubview:self.pm];
    
    [self initWeatherDownViews];
    
}

- (void)initWeatherDownViews {
    
    self.downView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 64 - 253 * (HEIGHT - 64) / 603, WIDTH, 253 * (HEIGHT - 64) / 603)];
    self.downView.backgroundColor = [UIColor lightGrayColor];
    self.downView.alpha = 0.2;
    [self.bgImageView addSubview:self.downView];
    
    for (int i = 0; i < 3; i++) {
        
        TBCLocalWeather *localWeather = [[TBCLocalWeather alloc] init];
        localWeather = [self.weather.allWeatherArray objectAtIndex:i + 1];
        
        UILabel *weekLabel = [[UILabel alloc] initWithFrame:CGRectMake((40 + 125 * i) * WIDTH / 375, HEIGHT - 64 - 223 * (HEIGHT - 64) / 603, 100 * WIDTH / 375, 30 * (HEIGHT - 64) / 603)];
        weekLabel.text = localWeather.week;
        weekLabel.textColor = [UIColor whiteColor];
        weekLabel.font = [UIFont systemFontOfSize:16];
        [self.bgImageView addSubview:weekLabel];
        
        UIImageView *weatherIV = [[UIImageView alloc] initWithFrame:CGRectMake((40 + 125 * i) * WIDTH / 375, HEIGHT - 64 - 183 * (HEIGHT - 64) / 603, 50 * WIDTH / 375, 68 * (HEIGHT - 64) / 603)];
        if ([localWeather.climate isEqualToString:@"多云"]) {
            
            weatherIV.image = [UIImage imageNamed:@"sun_and_cloud0048"];
            
        }else if ([localWeather.climate isEqualToString:@"晴"]) {
            
            weatherIV.image = [UIImage imageNamed:@"sun0101"];
            
        }else if ([localWeather.climate containsString:@"雨"]) {
            
            weatherIV.image = [UIImage imageNamed:@"rain0062"];
            
        }else {
            
            weatherIV.image = [UIImage imageNamed:@"cloud0029"];
            
        }
        [self.bgImageView addSubview:weatherIV];
        
        UILabel *temLabel = [[UILabel alloc] initWithFrame:CGRectMake((25 + 125 * i) * WIDTH / 375, HEIGHT - 64 - 123 * (HEIGHT - 64) / 603, 100 * WIDTH / 375, 30 * (HEIGHT - 64) / 603)];
        temLabel.text = localWeather.temperature;
        temLabel.textColor = [UIColor whiteColor];
        temLabel.font = [UIFont systemFontOfSize:18];
        [self.bgImageView addSubview:temLabel];
        
        UILabel *climateLabel = [[UILabel alloc] initWithFrame:CGRectMake((38 + 125 * i) * WIDTH / 375, HEIGHT - 64 - 88 * (HEIGHT - 64) / 603, 50 * WIDTH / 375, 30 * (HEIGHT - 64) / 603)];
        climateLabel.text = localWeather.climate;
        climateLabel.textAlignment = NSTextAlignmentCenter;
        climateLabel.textColor = [UIColor whiteColor];
        climateLabel.font = [UIFont systemFontOfSize:13];
        [self.bgImageView addSubview:climateLabel];
        
        UILabel *windLabel = [[UILabel alloc] initWithFrame:CGRectMake((45 + 125 * i) * WIDTH / 375, HEIGHT - 64 - 58 * (HEIGHT - 64) / 603, 100 * WIDTH / 375, 30 * (HEIGHT - 64) / 603)];
        windLabel.text = localWeather.wind;
        windLabel.textColor = [UIColor whiteColor];
        windLabel.font = [UIFont systemFontOfSize:13];
        [self.bgImageView addSubview:windLabel];
        
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
